/*
 * $Id: MagmonGuidanceLoader.java,v 1.6 2009/09/09 16:43:30 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.shemjob.guidance.loader;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.CfgXmlParserInfo;
import com.ge.healthcare.autosc.common.exceptions.XmlParserException;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.common.util.PropertyConstants;
import com.ge.healthcare.autosc.common.util.xml.dom.DomHelper;
import com.ge.healthcare.autosc.processor.PrcCodeConstants;
import com.ge.healthcare.autosc.processor.exception.GuidanceLoaderException;
import com.ge.healthcare.autosc.processor.shemjob.guidance.builder.ChannelDefListBuilder;
import com.ge.healthcare.autosc.processor.shemjob.guidance.builder.MagmonDataSetBuilder;
import com.ge.healthcare.autosc.processor.shemjob.guidance.builder.MinorEventListBuilder;
import com.ge.healthcare.autosc.processor.shemjob.guidance.dataset.MagmonDataSet;
import com.ge.healthcare.autosc.processor.shemjob.guidance.dataset.MagnetDataChannelDefList;
import com.ge.healthcare.autosc.processor.shemjob.guidance.dataset.MinorEvent;

/**
 * This loader manages Magnet Monitor guidance
 * 
 * @author 212042946
 * 
 */
public class MagmonGuidanceLoader extends GuidanceLoader {

	private HashMap<String, Object> datasetMap = null;
	private HashMap<String, Object> eventMap = null;
	private HashMap<String, Object> channelDefListMap = null;

	/**
	 * Constructor
	 * 
	 * @throws GuidanceLoaderException
	 */
	public MagmonGuidanceLoader() throws GuidanceLoaderException {
		super();
	}

	/**
	 * For Junit test. DO NOT USE FOR NON-JUNIT TEST
	 * 
	 * @throws GuidanceLoaderException
	 */
	public void loadForTest() throws GuidanceLoaderException {
		testFlag = true;
		load();
	}

	/**
	 * Build magnet monitor guidance
	 */
	public void load() throws GuidanceLoaderException {
		String configFileName = ApplicationInfo.getInstance().getProperty(
				PropertyConstants.MAGMON_GUIDANCE_FILE, "MagmonGuidance.xml");
		super.build(configFileName);
		loadDataSetMap(configFileName);
		loadEventMap(configFileName);
		loadChannelDefListMap(configFileName);
	}

	/**
	 * Get Magnet Monitor data set. Please refere to guidance file.
	 * 
	 * @param key
	 *            Identifier for the data set
	 * @return MagmonDataSet
	 */
	public MagmonDataSet getMagmonDataSet(String key) {
		MagmonDataSet magmonDataSet = (MagmonDataSet) datasetMap.get(key);
		if (magmonDataSet == null) {
			magmonDataSet = (MagmonDataSet) datasetMap.get("");
		}
		return magmonDataSet;
	}

	/**
	 * Get Magnet Monitor event. Please refer to guidance file.
	 * 
	 * @param majorFunction
	 *            Identifier for a group of event
	 * @param minorFunction
	 *            Identifier for a particular event within the majorFunction
	 * @return MinorEvent
	 */
	public MinorEvent getMinorEvent(String majorFunction, String minorFunction) {
		Map<?, ?> minorEventMap = (Map<?, ?>) eventMap.get(majorFunction);
		if (minorEventMap == null) {
			minorEventMap = (Map<?, ?>) eventMap.get("");
		}
		MinorEvent minorEvent = (MinorEvent) minorEventMap.get(minorFunction);
		if (minorEvent == null) {
			minorEvent = (MinorEvent) minorEventMap.get("");
		}
		return minorEvent;
	}

	/**
	 * Get Magnet Monitor channel definitions. Please refer to guidance file.
	 * 
	 * @param name
	 *            Identifier for the channels
	 * @return MagnetDataChannelDefList
	 */
	public MagnetDataChannelDefList getMagnetDataChannelDefList(String name) {
		MagnetDataChannelDefList channelDefList = (MagnetDataChannelDefList) channelDefListMap
				.get(name);
		if (channelDefList == null) {
			channelDefList = (MagnetDataChannelDefList) channelDefListMap
					.get("");
		}
		return channelDefList;
	}

	@Override
	protected String getParserName() {
		return CfgXmlParserInfo.MAG_MON_GUIDANCE;
	}

	private void loadDataSetMap(String configFileName)
			throws GuidanceLoaderException {
		if (ASCUtil.isNullOrEmptyMap(datasetMap)) {
			try {
				Node sectionNode = DomHelper.getSectionNode(doc, "DataSetList");
				datasetMap = buildConfigHash(sectionNode, "DataSetList",
						"DataSet", "DataSetName", "DefaultDataSet",
						"DataSetGuidance", new MagmonDataSetBuilder());
			} catch (XmlParserException xpe) {
				throw new GuidanceLoaderException(
						"Failed building dataset guidance from "
								+ configFileName, xpe, PrcCodeConstants.PRC_F_SHM_018);
			}
		}
	}

	private void loadEventMap(String configFileName)
			throws GuidanceLoaderException {
		if (ASCUtil.isNullOrEmptyMap(eventMap)) {
			try {
				Node sectionNode = DomHelper.getSectionNode(doc,
						"MajorEventList");
				eventMap = buildConfigHash(sectionNode, "MajorEventList",
						"MajorEvent", "MajorEventName", "DefaultMajorEvent",
						"MinorEventList", new MinorEventListBuilder());
			} catch (XmlParserException xpe) {
				throw new GuidanceLoaderException(
						"Failed building dataset guidance from "
								+ configFileName, xpe, PrcCodeConstants.PRC_F_SHM_018);
			}
		}
	}

	private void loadChannelDefListMap(String configFileName)
			throws GuidanceLoaderException {
		if (ASCUtil.isNullOrEmptyMap(channelDefListMap)) {
			try {
				Node sectionNode = DomHelper.getSectionNode(doc,
						"ChannelDefListList");
				channelDefListMap = buildConfigHash(sectionNode,
						"ChannelDefListList", "ChannelDefList",
						"ChannelDefListName", "DefaultChannelDefList",
						"ChannelDefArray", new ChannelDefListBuilder());
			} catch (XmlParserException xpe) {
				throw new GuidanceLoaderException(
						"Failed building dataset guidance from "
								+ configFileName, xpe, PrcCodeConstants.PRC_F_SHM_018);
			}
		}
	}
}
